clear all
use  "../data/zip_year_data", clear

*Define CHTV and CHTC and the IV 
gen lnchtc = log((chinese100cash - american100cash/american100*chinese100)*(american100/(american100 - american100cash)))
gen chtc = (chinese100cash - american100cash/american100*chinese100)*(american100/(american100 - american100cash)) 

gen lnchtv = log((chinese100_cashval - american100_cashval/american100_val*chinese100_val)*(american100_val/(american100_val - american100_cashval)))
gen chtv = (chinese100_cashval - american100_cashval/american100_val*chinese100_val)*(american100_val/(american100_val - american100_cashval))

by year, sort: egen chttc = sum(chtc)
gen chi_iv_ct = log(chineseper * chttc)
by year, sort: egen chttv = sum(chtv)
gen chi_iv_val = log(chineseper * chttv)

*Define the interacted terms
gen lnchtvXpost = lnchtv * post
gen lnchtcXpost = lnchtc * post
gen chi_iv_ctXpost = chi_iv_ct * post
gen chi_iv_valXpost = chi_iv_val * pos

drop if lnchtc == .
drop if lnchtv == .

*Run regression to get just the sample we have in table 1 column 1
ivreghdfe lnemp lnpopulation d_lnemp_9600 bachelorshare d_lnincome_9801 ln_density near_college_5  (lnchtv lnchtvXpost = chi_iv_val chi_iv_valXpost) if chtv != 0, absorb(timecounty) cluster(zipcode) 
keep if e(sample)

replace chtv = chtv/1000000
replace sum_val_tran = sum_val_tran/1000000
gen ln_sr_val_tran = log(sr_val_tran)
rename p_singlefam_real zhvi_real
replace zhvi_real = zhvi_real/1000000

gen lnchtv_bot = lnchtv if decile <=8
gen lnchtv_top = lnchtv if decile > 8
gen lnchtc_bot = lnchtc if decile <=8
gen lnchtc_top = lnchtc if decile > 8


label var chtv "CHTV (\$M)"
label var chtc "CHTC"
label var lnchtv "ln(CHTV)"
label var lnchtc "ln(CHTC)"
label var lnzhvi_real "Log house price"
label var lnemp "Log total employment"
label var lnemp12 "Log non-tradable employment"
label var lnemp2 "Log of tradable employment"
label var lnest "Log establishments"
label var ln_avgincome "Log household income"
label var ln_returns "Log number of tax filings"
label var lnchtv_bot "ln(CHTV) - Bottom-eight-deciles"
label var lnchtc_bot "ln(CHTC) - Bottom-eight-deciles"
label var lnchtv_top "ln(CHTV) - Top-two-deciles"
label var lnchtc_top "ln(CHTC) - Top-two-deciles"
label var sum_val_tran "Total housing transaction value (\$M)"
label var ct "Total housing transaction count"

*columns 1-2 (whole sample)
estpost sum chtv chtc lnchtv lnchtc sum_val_tran ct ///
lnchtv_top lnchtc_top lnchtv_bot lnchtc_bot ///
lnemp lnemp12 lnemp2 lnest lnzhvi_real ln_returns ln_avgincome , d
esttab using "../results/table1_12.tex", replace  varwidth(44)  cells("mean(fmt(%12.2fc)) sd(fmt(%12.2fc))") unstack noobs label nonumber


*columns 3-4 (2001-2007)
estpost sum chtv chtc lnchtv lnchtc sum_val_tran ct ///
lnchtv_top lnchtc_top lnchtv_bot lnchtc_bot ///
lnemp lnemp12 lnemp2 lnest lnzhvi_real ln_returns ln_avgincome if year < 2008 , d 
esttab using "../results/table1_34.tex", replace  varwidth(44)  cells("mean(fmt(%12.2fc)) sd(fmt(%12.2fc))") unstack noobs label nonumber

*columns 5-6 (2008-2013)
estpost sum chtv chtc lnchtv lnchtc sum_val_tran ct ///
lnchtv_top lnchtc_top lnchtv_bot lnchtc_bot ///
lnemp lnemp12 lnemp2 lnest lnzhvi_real ln_returns ln_avgincome if year >= 2008, d 
esttab using "../results/table1_56.tex", replace  varwidth(44)  cells("mean(fmt(%12.2fc)) sd(fmt(%12.2fc))") unstack noobs label nonumber

